﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace XRPGLibrary.Controls
{
    public class ControlManager : List<Control>//继承自控件列表，控件管理类实际上就是将页面上的控件以数列的形式存储，在已有操作的基础上添加一些针对控件的操作
    {
        #region Fields and Properties
        int selectedControl = 0; //当前选中控件序号
        static SpriteFont spriteFont;//XNA中用于绘制文字的对象
        public static SpriteFont SpriteFont
        {
            get { return spriteFont; }
        }
        #endregion
        #region Event Region
        public event EventHandler FocusChanged;
        #endregion
        
        #region Constructors构造函数
        public ControlManager(SpriteFont spriteFont)
            : base()
        {
            ControlManager.spriteFont = spriteFont;
        }
        public ControlManager(SpriteFont spriteFont, int capacity) //利用元素个数构造
            : base(capacity)
        {
            ControlManager.spriteFont = spriteFont;
        }
        public ControlManager(SpriteFont spriteFont, IEnumerable<Control> collection) : //利用控件对象构造
            base(collection)
        {
            ControlManager.spriteFont = spriteFont;
        }
        #endregion
        #region Methods对页面上的控件进行更新操作
        public void Update(GameTime gameTime, PlayerIndex playerIndex)
        {
            if (Count == 0)
                return;
            foreach (Control c in this)
            {
                if (c.Enabled)
                    c.Update(gameTime);
                if (c.HasFocus)
                    c.HandleInput(playerIndex);
            }
            if (
            InputHandler.KeyPressed(Keys.Up))
                PreviousControl();//选中上一个控件
            if (
            InputHandler.KeyPressed(Keys.Down))
                NextControl();//选中下一个控件
        }
        public void Draw(SpriteBatch spriteBatch)//对控件的绘制
        {
            foreach (Control c in this)
            {
                if (c.Visible)
                    c.Draw(spriteBatch);
            }
        }
        public void NextControl()
        {
            if (Count == 0)
                return;
            int currentControl = selectedControl;
            this[selectedControl].HasFocus = false;
            do
            {
                selectedControl++;
                if (selectedControl == Count)
                    selectedControl = 0;
                if (this[selectedControl].TabStop && this[selectedControl].Enabled)
                {
                    if (FocusChanged != null)
                        FocusChanged(this[selectedControl], null);
                    break;
                }
            } while (currentControl != selectedControl);
            this[selectedControl].HasFocus = true;
        }
        public void PreviousControl()
        {
            if (Count == 0)
                return;
            int currentControl = selectedControl;
            this[selectedControl].HasFocus = false;
            do
            {
                selectedControl--;
                if (selectedControl < 0)
                    selectedControl = Count - 1;
                if (this[selectedControl].TabStop && this[selectedControl].Enabled)
                {
                    if (FocusChanged != null)
                        FocusChanged(this[selectedControl], null);
                    break;
                }
            } while (currentControl != selectedControl);
            this[selectedControl].HasFocus = true;
        }
        #endregion
    }
}